<template>
	<view class="container">
		<tui-tabs :tabs="tabs" :isFixed="scrollTop>=0" :currentTab="currentTab" selectedColor="#E41F19" sliderBgColor="#E41F19"
		 @change="change"></tui-tabs>
		 
		<view :class="{'tui-order-list':scrollTop>=0}">
			<view class="tui-order-item" v-for="(order,orderIndex) in dataList[currentTab].orders" :key="orderIndex">
				<tui-list-cell :hover="false" :lineLeft="false" >
					<view class="tui-goods-title">
						<view>订单号：{{order.order_sn}}</view>
					</view>
				</tui-list-cell>

					<tui-list-cell padding="0" v-if="order.leixing=='0'">
						<!-- <view class="tui-goods-item" @tap="detail(order)"> -->
						<view class="tui-goods-item">
							<image :src="order.product.thumbnail" class="tui-goods-img"></image>
							<view class="tui-goods-center">
								<view class="tui-goods-name">{{order.product.title}}</view>
								<view class="tui-goods-attr">{{order.product.place}}</view>
								<view class="tui-goods-attr">活动</view>
							</view>
						</view>
					</tui-list-cell>
					
					<tui-list-cell padding="0" v-else>
						<!-- <view class="tui-goods-item" @tap="detail(order)"> -->
						<view class="tui-goods-item">
							<image :src="order.jiudian.thumbnail" class="tui-goods-img"></image>
							<view class="tui-goods-center">
								<view class="tui-goods-name">{{order.jiudian.name}}</view>
								<view class="tui-goods-attr">{{order.jiudian.address}}</view>
								
								<view class="tui-goods-attr">酒店房间: <text>{{order.room.title}}</text><text>{{order.totaldays}}晚</text></view>
								<view class="tui-goods-attr">入住时间: <text>{{order.checkintime}}</text>离开时间<text>{{order.departuretime}}</text></view>
							</view>
						</view>
					</tui-list-cell>

				<tui-list-cell :last="true">
					<view class="tui-goods-price">
						<view>合计：</view>
						<view class="tui-size-24">￥</view>
						<view class="tui-price-large">{{order.total_price}}</view>
					</view>
				</tui-list-cell>
				<view class="tui-order-btn" >
					<view class="tui-btn-ml" @tap="pay(dataList[currentTab].orders[orderIndex])" v-if="order.pay_status == 0">
						<tui-button type="danger" :plain="true" width="148rpx" height="56rpx"  :size="26" shape="circle">立即付款</tui-button>
					</view>
					<view class="tui-btn-ml" @tap="confirm(dataList[currentTab].orders[orderIndex].id)" v-if="order.status == 1">
						<tui-button type="danger" :plain="true" width="148rpx" height="56rpx"  :size="26" shape="circle">确认订单</tui-button>
					</view>
				</view>
			</view>
		</view>
		<!--加载loadding-->
		<tui-loadmore :visible="dataList[currentTab].loadding" :index="3" type="red"></tui-loadmore>
		<tui-nomore :visible="!dataList[currentTab].pullUpOn" bgcolor="#fafafa"></tui-nomore>
		<!--加载loadding-->

	</view>
</template>

<script>
	var api = require('@/common/api.js')
	import tuiTabs from "@/components/tui-tabs/tui-tabs"
	import tuiButton from "@/components/extend/button/button"
	import tuiLoadmore from "@/components/loadmore/loadmore"
	import tuiNomore from "@/components/nomore/nomore"
	import tuiListCell from "@/components/list-cell/list-cell"
	import tuiSticky from "@/components/sticky/sticky"
	import tuiIcon from "@/components/icon/icon"
	export default {
		components: {
			tuiTabs,
			tuiButton,
			tuiLoadmore,
			tuiNomore,
			tuiListCell,
			tuiSticky,
			tuiIcon
		},
		data() {
			return {
				tabs: [{
					name: "全部"
				}, {
					name: "待付款"
				}, {
					name: "待确认"
				}, {
					name: "已完成"
				}],
				currentTab: 0,
				scrollTop: 0,
				dataList:[{
					orders:[],
					pageIndex:1,
					pullUpOn:true,
					loadding:false
				},{
					orders:[],
					pageIndex:1,
					pullUpOn:true,
					loadding:false
				},{
					orders:[],
					pageIndex:1,
					pullUpOn:true,
					loadding:false
				},{
					orders:[],
					pageIndex:1,
					pullUpOn:true,
					loadding:false
				},{
					orders:[],
					pageIndex:1,
					pullUpOn:true,
					loadding:false
				}],
				userinfo:'',
				status:['待付款','待发货','待收货','待评价','已完成','已取消'],
				statusO:['待付款','待取货','待取货','待评价','已完成','已取消']
			}
		},
		onLoad(options){
			if(this.tui.isLogin()){
				this.userinfo = uni.getStorageSync("user")
			}else{
				uni.showToast({
					title:"您没有登陆，请先登录",
					icon:"none"
				})
				setTimeout(function(){
					uni.navigateTo({
						url:"/pages/mall-extend/my/my"
					})
				},1000)
			}
			if(options.index){
				this.currentTab = options.index
			}
			
			this.loadList(this.currentTab);
		},
		methods: {
			comment(){
				uni.navigateTo({
					url: '/pages/mall-extend/comment/comment'
				})
			},
			change(e) {
				this.currentTab = e.index
				this.loadList(e.index);
			},
			detail(item) {
				uni.navigateTo({
					url: '/pages/mall-extend/orderDetail/orderDetail?query='+JSON.stringify(item)
				})
			},


			//去支付
			pay(item){
				uni.login({
				  provider: 'weixin',
				  success: function (res) {
					if(res.code){
						api.post({
							url:'order/orderPay',
							data:item,
							success:(res)=>{
								var info = res.data;
								//调用微信支付api
								 uni.requestPayment({
									 provider: 'wxpay',
									 timeStamp: info.timeStamp,
									 nonceStr: info.nonceStr,
									 package: info.package,
									 signType: info.signType,
									 paySign: info.paySign, //签名，这个要提前获取一下
									 success: function (res) {
									 	//跳转到支付成功的订单页面
										uni.navigateTo({
											url:'/pages/mall-extend/success/success'
										})
									 },
									 fail: function (err) {
										 uni.showToast({
											 title:'支付失败',
											 icon:'none'
										 })
									 }
								 }) 
							}
						})
					}
				  },
				  fail:function(res){
					  console.log(res);
				  }
				});
			},
			loadList(index){
				if(this.dataList[this.currentTab].pullUpOn == false){
					return false;
				}
				api.get({
					url:"order/myorder",
					data:{
						page:this.dataList[this.currentTab].pageIndex+',8',
						user_id:this.userinfo.id,
						type:index
					},
					success: res => {
						let news = res.data;
						this.dataList[this.currentTab].pageIndex++
						if(news.length < 8){
							this.dataList[this.currentTab].pullUpOn = false;
						}
						this.dataList[this.currentTab].orders = this.dataList[this.currentTab].orders.concat(news);
						this.dataList[this.currentTab].loadding = false;
					}
				})
			}
			
		},
		onPullDownRefresh() {
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 200);
		},
		onReachBottom() {
			//只是测试效果，逻辑以实际数据为准
			if(this.dataList[this.currentTab].pullUpOn == false){
				return false;
			}
			this.dataList[this.currentTab].loadding = true
			
			this.loadList(this.currentTab);
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		}
	}
</script>

<style>
	.container {
		padding-bottom: env(safe-area-inset-bottom);
	}
	.tui-pro-price {
		padding-top: 18rpx;
	}
	
	.tui-sale-price {
		font-size: 34rpx;
		font-weight: 500;
		color: #e41f19;
	}
	.tui-order-list {
		margin-top: 80rpx;
	}

	.tui-order-item {
		margin-top: 20rpx;
		border-radius: 10rpx;
		overflow: hidden;
	}

	.tui-goods-title {
		width: 100%;
		font-size: 28rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.tui-order-status {
		color: #888;
		font-size: 26rpx;
	}

	.tui-goods-item {
		width: 100%;
		padding: 20rpx 30rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
	}

	.tui-goods-img {
		width: 180rpx;
		height: 180rpx;
		display: block;
		flex-shrink: 0;
	}

	.tui-goods-center {
		flex: 1;
		padding: 10rpx 8rpx;
		box-sizing: border-box;
	}

	.tui-goods-name {
		padding-bottom: 10rpx;
		max-width: 500rpx;
		word-break: break-all;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		font-size: 30rpx;
		line-height: 32rpx;
	}

	.tui-goods-attr {
		font-size: 22rpx;
		color: #888888;
		line-height: 32rpx;
		padding-top: 2rpx;
		word-break: break-all;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
	}

	.tui-price-right {
		text-align: right;
		font-size: 24rpx;
		color: #888888;
		line-height: 30rpx;
		padding-top: 20rpx;
	}

	.tui-color-red {
		color: #E41F19;
		padding-right: 30rpx;
	}

	.tui-goods-price {
		width: 100%;
		display: flex;
		align-items: flex-end;
		justify-content: flex-end;
		font-size: 24rpx;
	}

	.tui-size-24 {
		font-size: 24rpx;
		line-height: 24rpx;
	}

	.tui-price-large {
		font-size: 32rpx;
		line-height: 30rpx;
		font-weight: 500;
	}

	.tui-order-btn {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: flex-end;
		background: #fff;
		padding: 10rpx 30rpx 20rpx;
		box-sizing: border-box;
	}

	.tui-btn-ml {
		margin-left: 20rpx;
	}
</style>
